const session = require("cookie-session");
const locals = require("./system/locals");
const bodyParser = require("body-parser");
const express = require("express");
const system = require("./system");
const KEY = require("../config/Key");
const corsConfig = require("./system/corsConfig");
const cookieParser = require('cookie-parser')(KEY)
const cors = require('cors')(corsConfig);
const path = require('path')
const replaceBaseURL = require('./system/replaceBaseURL')
const historyApiFallback = require("connect-history-api-fallback");
class AppConfig {
    constructor(app) {
        app.use(session({
            httpOnly: true,
            maxAge: 1000 * 60 * 60 * 24,
            keys: ['1', '2'],
        }))
        replaceBaseURL(process.env.VUE_APP_DEV_API, process.env.VUE_APP_PROD_API)
        app.use(locals({
            pic_count: {},
            version: "v1.3.4"
        }))
        // 解析post表单数据的中间件
        app.use(bodyParser.urlencoded({extended: false}))
        app.use(cookieParser)
        //引入json返回格式
        app.use(express.json())

        app.use(system)
        //使用cors插件解决跨域问题
        app.use(cors);

        app.use(historyApiFallback())
        app.use(function (req, res, next) {
            let app = req.app
            let distPath = path.join(__dirname, '../', '/dist')
            let staticPath = path.join(__dirname, '../', '/static')
            //vue的静态打包文件夹 后台管理页面
            app.use('/', express.static(distPath, {
                index: 'index.html'
            }))
            //存放静态资源的文件夹
            app.use('/static', express.static(staticPath))
            next()
        })

    }
}

module.exports = AppConfig
